﻿using System;
using System.Web;
using Jumpton.SFA.Server.BusinessRule;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;

public class SmallImage : IHttpHandler {

    private static String DefaultPhotoFile = null;
    private static Stream DefaultStream;
    public void ProcessRequest(HttpContext context)
    {
        if (DefaultPhotoFile == null)
            DefaultPhotoFile = context.Server.MapPath("images/nophoto.jpg");
        context.Response.ClearContent();
        context.Response.ContentType = "image/Gif";
        String ImagePath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
        String FileName= null;
        int height = 80, width = 80;
        int.TryParse(context.Request.QueryString["H"], out height);
        int.TryParse(context.Request.QueryString["W"], out width);
        String id = context.Request.QueryString["ID"];
        
        try
        {

            var photo = new PhotoManager().Load(id);
            if (photo != null)
            {
                FileName = ImagePath + "\\" + photo.PHOTOPATH;
                if (!File.Exists(FileName))
                {
                    FileName = DefaultPhotoFile;

                }
                

            }
            

        }
        catch
        {
            
        }
        if(FileName == null)
            //输出默认图片
            FileName = DefaultPhotoFile;
        Stream stream  = null;
        try
        {
            if (FileName.Equals(DefaultPhotoFile) && DefaultStream != null)
            {
                stream = DefaultStream;
            }
            else
            {
                stream = PicSized(FileName, width, height, ImageFormat.Png);
                if (FileName.Equals(DefaultPhotoFile))
                    DefaultStream = stream;
            }
            byte[] data = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(data, 0, data.Length);
            context.Response.BinaryWrite(data);
            context.Response.Flush();
        }
        finally
        {
            if (stream != null && stream != DefaultStream)
            {
                stream.Close();
                stream.Dispose();
                stream = null;
            }
        }

    }

    /// <summary> 
    /// 放大缩小图片尺寸 
    /// </summary> 
    /// <param name="picPath"></param> 
    /// <param name="reSizePicPath"></param> 
    /// <param name="iSize"></param> 
    /// <param name="format"></param> 
    public Stream PicSized(String FileName, int width, int height, ImageFormat format)
    {
        MemoryStream ms = new MemoryStream();
        Bitmap originBmp = new Bitmap(FileName);
        if (width > originBmp.Width || height >originBmp.Height)
        {
            width = originBmp.Width;
            height = originBmp.Height;
        }
        else
        {
            float i = (float)originBmp.Width / (float)originBmp.Height;
            if (i > 1)
                height = (int)((float)width / i);
            else
                height = (int)((float)width * i);
        }
        Bitmap resizedBmp = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(resizedBmp);
        //设置高质量插值法   
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度   
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //消除锯齿 
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        g.DrawImage(originBmp, new Rectangle(0, 0, width, height), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);
        resizedBmp.Save(ms, format);
        g.Dispose();
        resizedBmp.Dispose();
        originBmp.Dispose();

        return ms;
    }
   
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}